//-----------------------------------------------------------------------
// <copyright file="a_ReferDetailDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_ReferDetailDAOBase
    {
        #region Common methods
        public virtual a_ReferDetail Createa_ReferDetailFromReader(IDataReader reader)
        {
            a_ReferDetail item = new a_ReferDetail();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("RefDetID"))) item.RefDetID = (int)reader["RefDetID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RefID"))) item.RefID = (int)reader["RefID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateVisit"))) item.DateVisit = (DateTime)reader["DateVisit"];
                if (!reader.IsDBNull(reader.GetOrdinal("TimeVisit"))) item.TimeVisit = (decimal)reader["TimeVisit"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_ReferDetail methods
            
        public virtual void Createa_ReferDetail(a_ReferDetail a_ReferDetail)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferDetail_ins");
                
                database.AddInParameter(dbCommand, "@RefID", DbType.Int32, a_ReferDetail.RefID);
                database.AddInParameter(dbCommand, "@DateVisit", DbType.DateTime, a_ReferDetail.DateVisit);
                database.AddInParameter(dbCommand, "@TimeVisit", DbType.Currency, a_ReferDetail.TimeVisit);
                database.AddOutParameter(dbCommand, "@RefDetID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_ReferDetail.RefDetID = (int)database.GetParameterValue(dbCommand, "@RefDetID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_ReferDetail methods
        
        public virtual void Updatea_ReferDetail(a_ReferDetail a_ReferDetail)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferDetail_upd");            
                
                database.AddInParameter(dbCommand, "@RefDetID", DbType.Int32, a_ReferDetail.RefDetID);
                database.AddInParameter(dbCommand, "@RefID", DbType.Int32, a_ReferDetail.RefID);
                database.AddInParameter(dbCommand, "@DateVisit", DbType.DateTime, a_ReferDetail.DateVisit);
                database.AddInParameter(dbCommand, "@TimeVisit", DbType.Currency, a_ReferDetail.TimeVisit);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_ReferDetail methods
        public virtual void Deletea_ReferDetail(int refDetID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferDetail_del");
                
                database.AddInParameter(dbCommand, "@RefDetID", DbType.Int32, refDetID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_ReferDetail methods
        
        public virtual a_ReferDetail Geta_ReferDetail(int refDetID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferDetail_sel");
                
                database.AddInParameter(dbCommand, "@RefDetID", DbType.Int32, refDetID);
                
                a_ReferDetail a_ReferDetail =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_ReferDetail = Createa_ReferDetailFromReader(reader);
                        reader.Close();
                    }
                }
                return a_ReferDetail;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_ReferDetailList methods
        public virtual a_ReferDetailCollection Geta_ReferDetailList(a_ReferDetailColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferDetail_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_ReferDetailCollection a_ReferDetailCollection = new a_ReferDetailCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_ReferDetail a_ReferDetail = Createa_ReferDetailFromReader(reader);
                        a_ReferDetailCollection.Add(a_ReferDetail);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ReferDetailCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_ReferDetailCollection Geta_ReferDetailList(a_ReferDetailColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_ReferDetailList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}